package interfaces.hud.topWindows;

import logic.ships.mothership.MotherShip;
import logic.status.StatusChangedEvent;
import logic.status.StatusListener;
import fileHandling.language.interfaces.HUDText;
import interfaces.GUISource;
import interfaces.PropertyContent;
import interfaces.hud.BasicHUD;
import interfaces.language.LanguageLabel;

import networking.messages.syncMessages.ValueType;

import org.fenggui.util.Color;

public class MotherShipsStatusHUD extends TopHUDWindow {
	
	private PropertyContent teamMSHP, teamMSShields, enemyMSHP, enemyMSShields;
	private int contentWidth, contentHeight, border;
	private MotherShip teamMS, enemyMS;
	private LanguageLabel teamMSLabel, enemyMSLabel;
	
	public MotherShipsStatusHUD(MotherShip teamMS, MotherShip enemyMS, BasicHUD hud) {
		super(null, hud, hud.getWidth() / 2, hud.getHeight() / 15);
		
		this.teamMS = teamMS;
		this.enemyMS = enemyMS;
		
		border = content.getHeight() / 20;
		contentWidth = content.getWidth() / 4 - border * 2;
		contentHeight = content.getHeight() / 2 - border * 2;
		
		initLabels();
		initStatusBars();
		addListeners();
		
		layout();
	}
	
	private void initLabels() {
		int labelY = content.getHeight() - contentHeight - border * 2;
		int labelWidth = contentWidth * 2;
		
		teamMSLabel = new LanguageLabel(HUDText.Team_Mothership, GUISource.middleFont);
		teamMSLabel.setXY(border, labelY);
		teamMSLabel.setSize(labelWidth, contentHeight);
		addWidget(teamMSLabel);
		
		enemyMSLabel = new LanguageLabel(HUDText.Enemy_Mothership, GUISource.middleFont);
		enemyMSLabel.setXY(border * 4 + labelWidth, labelY);
		enemyMSLabel.setSize(labelWidth, contentHeight);	
		addWidget(enemyMSLabel);
	}
	
	private void initStatusBars() {
		int contentX = border;
		Color backgroundColor = Color.BLACK;
		
		teamMSShields = new PropertyContent(contentWidth, contentHeight, contentX, border);
		teamMSShields.changeBackgroundColor(backgroundColor);
		teamMSShields.changeValueColor(Color.BLUE);
		addWidget(teamMSShields);
		contentX += contentWidth;
		
		teamMSHP = new PropertyContent(contentWidth, contentHeight, contentX, border);
		teamMSHP.changeBackgroundColor(backgroundColor);
		teamMSHP.changeValueColor(GUISource.green);
		addWidget(teamMSHP);
		contentX += contentWidth + border * 4;
		
		enemyMSShields = new PropertyContent(contentWidth, contentHeight, contentX, border);
		enemyMSShields.changeBackgroundColor(backgroundColor);
		enemyMSShields.changeValueColor(Color.BLUE);
		addWidget(enemyMSShields);
		contentX += contentWidth;
		
		enemyMSHP = new PropertyContent(contentWidth, contentHeight, contentX, border);
		enemyMSHP.changeBackgroundColor(backgroundColor);
		enemyMSHP.changeValueColor(Color.RED);
		addWidget(enemyMSHP);
	}
	
	private void addListeners() {
		hud.addListener(teamMS, new StatusListener() {
			@Override
			public void valueChanged(StatusChangedEvent event) {
				ValueType type = event.getType();
				switch(type) {
					case HP: teamMSHP.setValue(event.getIntValue()); break;
					case Shield: teamMSShields.setValue(event.getIntValue()); break;
				}
			}
		});
		hud.addListener(enemyMS, new StatusListener() {
			@Override
			public void valueChanged(StatusChangedEvent event) {
				ValueType type = event.getType();
				switch(type) {
					case HP: enemyMSHP.setValue(event.getIntValue()); break;
					case Shield: enemyMSShields.setValue(event.getIntValue()); break;
				}
			}
		});
		
		teamMSHP.setMaxValue(teamMS.getMaxHP());
		enemyMSHP.setMaxValue(enemyMS.getMaxHP());
		teamMSShields.setMaxValue(teamMS.getMaxShield());
		enemyMSShields.setMaxValue(enemyMS.getMaxShield());
		
		teamMSHP.setValue(teamMS.getMaxHP());
		enemyMSHP.setValue(enemyMS.getMaxHP());
		teamMSShields.setValue(teamMS.getMaxShield());
		enemyMSShields.setValue(enemyMS.getMaxShield());
	}
}